﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml;
using GateKeeper;
using System.Collections.Generic;
using System.Globalization;

public partial class GateKeeper_User_Controls_HttpBL : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        gridHttpBL.RowDataBound += new GridViewRowEventHandler(gridHttpBL_RowDataBound);

        if (!Page.IsPostBack)
        { LoadData(); }
    }


    void gridHttpBL_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        // Dynamically hide the ID field so it's in the client data but not displayed
        e.Row.Cells[0].Visible = false; // Hide entryID
        e.Row.Cells[7].Visible = false; // Hide delete button for now...

        // Dynamically add confirmation to delete button
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Button btn = (Button)e.Row.FindControl("btnDelete");
            if (btn != null) { btn.Attributes.Add("onClick", "return confirm('Are you sure you want to delete this entry?');"); }
        }
    }

    /// <summary>
    /// Binds to selected row and sends the id cell (0) to
    /// Utils.DeleteEntry for deletion based in the entryID
    /// </summary>
    protected void btnDelete_Click(object sender, EventArgs e)
    {
        Button btn = (Button)sender;
        GridViewRow grdRow = (GridViewRow)btn.Parent.Parent;
        Utils.DeleteEntry(XmlFileType.HttpBLList, grdRow.Cells[0].Text);
        Response.Redirect(Request.RawUrl, true);
    }

    void LoadData()
    {
        List<httpBL> entries = new List<httpBL>();
        try
        {
            foreach (XmlNode node in Utils.xmlHttpBLList.SelectSingleNode("/gatekeeper/httpbl").ChildNodes)
            {
                httpBL entry = new httpBL();
                entry.ID = node.Attributes["id"].InnerText ?? string.Empty;
                entry.Date = DateTime.Parse(node.Attributes["date"].InnerText ?? string.Empty, CultureInfo.InvariantCulture);
                entry.IPAddress = node.Attributes["ipaddress"].InnerText ?? string.Empty;
                entry.ThreatLevel = node.Attributes["threatlevel"].InnerText ?? string.Empty;
                entry.VisitorType = node.Attributes["visitortype"].InnerText ?? string.Empty;
                entry.LastActivity = node.Attributes["lastactivity"].InnerText ?? string.Empty;
                entry.UserAgent = HttpUtility.UrlDecode(node.Attributes["useragent"].InnerText ?? string.Empty);
                entry.Referrer = HttpUtility.UrlDecode(node.Attributes["referrer"].InnerText ?? string.Empty);

                // Make sure the ID is not empty - required field
                if (!string.IsNullOrEmpty(entry.ID))
                { entries.Add(entry); }
            }
            gridHttpBL.DataSource = entries;
            gridHttpBL.DataBind();

        }
        catch (Exception)
        {
            return;
        }
    }
}
